package web.admin.cms;

import domain.cms.CmsHomePage;
import domain.cms.CmsPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.admin.CmsAdminService;

import java.util.logging.Level;
import java.util.logging.Logger;


public final class CmsAdmin extends Panel {
    @SpringBean
    public CmsAdminService cmsService;
    Panel empty = new EmptyPanel("current");
    Panel current;
    Button currentButton;
    private Button createSectionButton;
    private static Logger logger = Logger.getLogger(CmsAdmin.class.getName());

    public CmsAdmin(String id) {
        super(id);
        add(new Link("homePage") {
            @Override
            public void onClick() {
                replacePanelHomePage();
            }
        });
        add(new Link("list") {
            @Override
            public void onClick() {
                replacePanelToList();
            }
        });
        current = new SectionList("current", this);
        add(current);

        Form form = new Form("createSection");

        createSectionButton = new Button("createSectionButton") {
            @Override
            public void onSubmit() {
                try {
                    cmsService.createCmsSection();
                } catch (Exception e) {
                    logger.log(Level.SEVERE, null, e);
                    error(getLocalizer().getString("system.error", this));
                }
            }
        };
        form.add(createSectionButton);

        currentButton = new Button("current");
        currentButton.setVisible(false);
        form.add(currentButton);
        add(form);


    }

    public void replacePanelToList() {
        createSectionButton.setVisible(true);

        createSectionButton.setVisible(true);
        Panel sectionList = new SectionList("current", this);
        current.replaceWith(sectionList);
        current = sectionList;
    }

    public void replacePanelToEmpty() {
        current.replaceWith(empty);
        current = empty;
    }

    void replacePanelHomePage() {
        hideButtons();
        IModel<CmsHomePage> homePage = new LoadableDetachableModel<CmsHomePage>() {

            @Override
            protected CmsHomePage load() {
                return cmsService.getHomePage();
            }
        };
        Panel userPanel = new PageEdit("current", homePage, this);
        current.replaceWith(userPanel);
        current = userPanel;
    }


    void replacePanelPage(IModel<CmsPage> page) {
        hideButtons();
        Panel userPanel = new PageEdit("current", page, this);
        current.replaceWith(userPanel);
        current = userPanel;
    }

    private void hideButtons() {
        createSectionButton.setVisible(false);
    }
}
